﻿using Dapper;
using Microsoft.Data.SqlClient;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Asion.Dapper
{

    public class SimpleCrudAsyncRepository<TEntity> : BaseRepository, ICrudAsyncRepository<TEntity> where TEntity : IEntity
    {


      /*  public async Task<int> InsertAsync(TEntity model)
        {
            var result=0;
            using (var connection = this.GetDbConnection<TEntity>())
            {
                result= await connection.InsertAsync<TEntity>(model);
            }
            return result;
        }


        public async Task<int> BatchInsertAsync(IList<TEntity> models, int count = DapperConfiguration.batch_insert_count)
        {
            if (models == null || !models.Any())
                throw new ArgumentNullException(nameof(models));
            var pageSize = count;
            var pageIndex = 0;
            int result = 0;
            while (true)
            {
                var temps = models.Skip(pageIndex * pageSize).Take(pageSize).ToList();
                pageIndex++;
                if (temps == null || !temps.Any())
                    break;

                using (var connection = this.GetDbConnection<TEntity>())
                {
                    result += await connection.BatchInsertAsync<TEntity>(models);
                }

            }
            return result;

        }


       


        public async Task<bool> DeleteAsync(IDictionary<string, string> dic)
        {
            int result=0;
            using (var connection = this.GetDbConnection<TEntity>())
            {
                result = await connection.DeleteAsync<TEntity>(dic);
            }
            return result > 0 ? true : false;
        }

        [Obsolete("don't suggest get all columns")]
        public async Task<TEntity> GetAsync(IDictionary<string, string> dic)
        {
            TEntity result;
            using (var connection = this.GetDbConnection<TEntity>())
            {
                result =await connection.GetAsync<TEntity>(dic);
            }
            return result;
        }

      


        public async Task<bool> UpdateAsync(TEntity model)
        {
            int result=0;
            using (var connection = this.GetDbConnection<TEntity>())
            {
                result = await connection.UpdateAsync<TEntity>(model);
            }
            return result > 0 ? true : false;
        }


        public async Task<bool> DeleteListAsync(string where, object parameters)
        {
            int? result;
            using (var connection = this.GetDbConnection<TEntity>())
            {
                result =await connection.DeleteListAsync<TEntity>(where, parameters);
            }
            return result > 0 ? true : false;
        }



        public async Task<IEnumerable<TEntity>> GetListAsync(string where, object parameters)
        {
            IEnumerable<TEntity> result;
            using (var connection = this.GetDbConnection<TEntity>())
            {
                result =await connection.GetListAsync<TEntity>(where, parameters);
            }
            return result;
        }


        public async Task<Page<TEntity>> GetListPageAsync(int pageNumber, int rowsPerPage, string conditions, string orderBy, object parameters)
        {
            Page<TEntity> result;
            using (var connection = this.GetDbConnection<TEntity>())
            {
                result =await connection.GetListPageAsync<TEntity>(pageNumber, rowsPerPage, conditions, orderBy, parameters: parameters);
            }
            return result;
        }

        public async Task<int> RecordCountAsync(string conditions = "", object parameters = null)
        {
            int result;
            using (var connection = this.GetDbConnection<TEntity>())
            {
                result =await connection.RecordCountAsync<TEntity>(conditions, parameters);
            }
            return result;
        }*/
    }
}
